package cfca.seal.web.action.crl;

import cfca.sadk.x509.certificate.X509Cert;
import cfca.seal.bean.AdminUser;
import cfca.seal.bean.OperationLog;
import cfca.seal.common.Page;
import cfca.seal.exception.CodeException;
import cfca.seal.mananger.OperationLogManager;
import cfca.seal.mananger.TrustCertManager;
import cfca.seal.util.StringUtil;
import cfca.seal.web.service.OperationLogService;
import cfca.seal.web.service.TrustCertService;
import cfca.seal.web.system.SessionManager;
import java.io.UnsupportedEncodingException;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

@Controller
@RequestMapping({"/trustCert"})
public class TrustCertAction
{
  private static final Logger log = LoggerFactory.getLogger("web_sys");
  private static final int DEFAULT_PAGE_NO = 1;
  private static final String DN = "DN";
  private String jumpListPage = "redirect:/trustCert/v_list.do";
  private String applicationName = "证书链管理";

  @Resource
  private TrustCertService trustCertService;

  @Resource
  private OperationLogService operationLogService;

  @RequestMapping({"/v_list"})
  public String trustCertList(ModelMap modelMap, HttpServletRequest request) throws CodeException { try { TrustCertManager.instance.refresh();
      String pageNoString = request.getParameter("pageNo");
      int pageNo = StringUtil.isNotEmpty(pageNoString) ? Integer.parseInt(pageNoString) : 1;
      List allList = TrustCertManager.instance.getTrustCertList();
      Page page = new Page(allList.size(), 100, pageNo);
      page.setBeans(allList.subList((int)(page.getStart() - 1L), (int)page.getEnd()));
      modelMap.put("page", page);
      return "sys/trustcert/input.html";
    } catch (Exception e) {
      log.error("证书链配置错误", e);
      throw new CodeException("C0001", "证书链配置错误", e);
    } }

  @RequestMapping({"/v_add"})
  public String sysAdd(ModelMap modelMap, HttpServletRequest request)
  {
    return "sys/trustcert/add.html";
  }

  @RequestMapping({"/o_save"})
  public String save(@RequestParam("trustCert") CommonsMultipartFile trustCert, ModelMap modelMap, HttpServletRequest request) throws CodeException
  {
    OperationLog operationLog = OperationLogManager.getInstance(request, this.applicationName, "增加", "trustCert.p7b", "增加证书链信息", null);
    operationLog.setOrganizationId(SessionManager.getManager(request).getOrganizationId());
    log.info("添加信任证书开始");
    try {
      this.trustCertService.importTrustCert(trustCert.getInputStream());
      log.info("添加信任证书结束");
      return this.jumpListPage;
    } catch (CodeException ce) {
      operationLog.setResult(10);
      log.error("导入信任证书异常", ce);
      throw new CodeException(ce.getCode(), ce.getMessage());
    } catch (Exception e) {
      operationLog.setResult(10);
      log.error("导入信任证书或更新配置时异常", e);
      throw new CodeException("C0001", "导入信任证书或更新配置时异常", e);
    } finally {
      this.operationLogService.addOperationLog(operationLog);
    }
  }

  @RequestMapping({"/v_view"})
  public String view(String viewDn, ModelMap map, HttpServletRequest request) throws CodeException {
    try {
      if (StringUtil.isEmpty(viewDn)) {
        throw new CodeException("C3042", "DN没有输入");
      }
      if (!StringUtil.checkSubjectDN(viewDn)) {
        throw new CodeException("C9012", "DN格式不正确");
      }
      viewDn = new String(viewDn.getBytes("ISO8859-1"), "UTF-8");
      X509Cert cert = TrustCertManager.instance.getTrustCertByDn(viewDn);
      if (cert == null) {
        log.error("无此信任证书,DN=" + viewDn);
        throw new CodeException("C0001", "无此信任证书,DN=" + viewDn);
      }
      map.put("cert", cert);
      return "sys/trustcert/detail.html";
    } catch (UnsupportedEncodingException e) {
      log.error("查看证书链详细信息失败", e);
      throw new CodeException("C0001", "UnsupportedEncodingException", e);
    }
  }

  @RequestMapping({"/v_delete"})
  public String viewDeletePage(String deledteDn, ModelMap modelMap) throws CodeException {
    try {
      if (StringUtil.isEmpty(deledteDn)) {
        throw new CodeException("C3042", "DN没有输入");
      }
      deledteDn = new String(deledteDn.getBytes("ISO8859-1"), "UTF-8");
      X509Cert cert = TrustCertManager.instance.getTrustCertByDn(deledteDn);
      if (cert == null) {
        throw new CodeException("C0001", "无此信任证书,DN=" + deledteDn);
      }

      modelMap.put("cert", cert);
      return "sys/trustcert/delete.html";
    } catch (UnsupportedEncodingException e) {
      log.error("UnsupportedEncodingException", e);
      throw new CodeException("C0001", "UnsupportedEncodingException", e);
    }
  }

  @RequestMapping({"/o_delete"})
  public String delete(String deledteDn, ModelMap modelMap, HttpServletRequest request) throws CodeException
  {
    OperationLog operationLog = OperationLogManager.getInstance(request, this.applicationName, "删除", "trustCert.p7b", "删除证书链信息", null);
    operationLog.setOrganizationId(SessionManager.getManager(request).getOrganizationId());
    log.info("删除信任证书开始");
    try {
      this.trustCertService.deleteTrustCert(deledteDn);
      log.info("删除信任证书结束");
      return this.jumpListPage;
    } catch (Exception e) {
      operationLog.setResult(10);
      log.error("删除信任证书或更新配置时异常", e);
      throw new CodeException("C0001", "删除信任证书或更新配置时异常", e);
    } finally {
      this.operationLogService.addOperationLog(operationLog);
    }
  }
}